/*
 * Created by JFormDesigner on Wed Jun 26 20:09:51 CST 2024
 */

package scct.ZhuZhouXing.ui;

import scct.ZhuZhouXing.dao.UserDao;
import scct.ZhuZhouXing.model.User;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author xzy
 */
public class LoginFrom extends JFrame {
    public LoginFrom() {
        initComponents();
    }

    private void btnlogin(ActionEvent e) {
        String name = txtname.getText();
        String pass = txtpass.getText();
        System.out.println(name + "," + pass);
        if (!name.matches("^\\w{2,10}$")) {
            JOptionPane.showMessageDialog(null, "您输入的用户名不合法，请检查后重试！");
        } else if (!pass.matches("^\\w{2,10}$")) {
            JOptionPane.showMessageDialog(null, "您输入的密码不合法，请检查后重试！");

        }
        UserDao userDao = new UserDao();
        User user = new UserDao().login(name, pass);
        if (user == null) {
            JOptionPane.showMessageDialog(this, "登录失败！");
        } else {
            JOptionPane.showMessageDialog(this, "登录成功！");
            MainFrom mainFrom = new MainFrom();
            mainFrom.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrom.setSize(800, 600);
            mainFrom.setLocationRelativeTo(null);
            mainFrom.setVisible(true);
            this.dispose();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - 周文成
        label1 = new JLabel();
        txtname = new JTextField();
        txtpass = new JTextField();
        btnlogin = new JButton();
        label2 = new JLabel();
        label3 = new JLabel();
        button2 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("LOGIN");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 18f));
        contentPane.add(label1);
        label1.setBounds(205, 35, 145, 40);
        contentPane.add(txtname);
        txtname.setBounds(165, 85, 210, 35);
        contentPane.add(txtpass);
        txtpass.setBounds(165, 130, 210, 35);

        //---- btnlogin ----
        btnlogin.setIcon(new ImageIcon(getClass().getResource("/icon/\u767b\u5f55.png")));
        btnlogin.addActionListener(e -> btnlogin(e));
        contentPane.add(btnlogin);
        btnlogin.setBounds(new Rectangle(new Point(140, 195), btnlogin.getPreferredSize()));

        //---- label2 ----
        label2.setIcon(new ImageIcon(getClass().getResource("/icon/\u7528\u6237.png")));
        label2.setText("\u8d26\u53f7");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 6f));
        contentPane.add(label2);
        label2.setBounds(80, 85, 77, 38);

        //---- label3 ----
        label3.setIcon(new ImageIcon(getClass().getResource("/icon/\u5bc6\u7801.png")));
        label3.setText("\u5bc6\u7801");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 6f));
        contentPane.add(label3);
        label3.setBounds(80, 130, 77, 38);

        //---- button2 ----
        button2.setIcon(new ImageIcon(getClass().getResource("/icon/\u6ce8\u518c.png")));
        contentPane.add(button2);
        button2.setBounds(275, 195, 85, 40);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(480, 345);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - 周文成
    private JLabel label1;
    private JTextField txtname;
    private JTextField txtpass;
    private JButton btnlogin;
    private JLabel label2;
    private JLabel label3;
    private JButton button2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
